package com.bnz.manager.service.impl;

import com.alibaba.fastjson.JSON;
import com.bnz.common.PageUtils;
import com.bnz.manager.entity.ItemEntity;
import com.bnz.manager.service.ItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bnz.common.Query;


import com.bnz.manager.dao.GoodsDao;
import com.bnz.manager.entity.GoodsEntity;
import com.bnz.manager.service.GoodsService;


@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private ItemService itemService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                new QueryWrapper<GoodsEntity>()
        );

        return new PageUtils(page);
    }

    // 商品审核
    @Override
    public void updateStatus(String id, String status) {
        //1. 第一步：完成商品审核
        GoodsEntity goodsEntity = new GoodsEntity();
        goodsEntity.setId(id);
        goodsEntity.setAuditStatus(status);
        this.updateById(goodsEntity);
        //2. 第二步：根据goods的商品id，查询得到对应的sku列表
        List<ItemEntity> items = itemService.list(new QueryWrapper<ItemEntity>().eq("goods_id", id));
        //3. 发送消息给搜索模块（充当监听消息方，得到数据后同步到elasticsearch中）
        rabbitTemplate.convertAndSend("updateStatus", JSON.toJSONString(items));

    }

}